package cn.aylog.controller;

import cn.aylog.pojo.Account;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class AccountController extends BaseServlet{

    /**
     * 登陆
     * @param request
     * @param response
     * @throws Exception
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Account account = this.wrapParam(request, Account.class);
        System.out.println(account);
        PrintWriter out = response.getWriter();
        Map<String,Object> map = new HashMap<>();
        map.put("isLogin", true);
        out.write(mapper.writeValueAsString(map));
    }

    /**
     * 注册
     * @param request
     * @param response
     * @throws Exception
     */
    public void register(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Account account = this.wrapParam(request, Account.class);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(account);
        PrintWriter out = response.getWriter();
        Map<String, Object> map = new HashMap<>();
        map.put("flag", true);
        out.write(mapper.writeValueAsString(map));
    }

    /**
     * 修改
     * @param request
     * @param response
     * @throws Exception
     */
    public void modify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Account account = this.wrapParam(request, Account.class);
        System.out.println(account);
        ObjectMapper mapper = new ObjectMapper();
        PrintWriter out = response.getWriter();
        Map<String, Object> map = new HashMap<>();
        map.put("flag", true);
        out.write(mapper.writeValueAsString(map));
    }

    /**
     * 查询
     * @param request
     * @param response
     * @throws Exception
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        List<Account> list = new ArrayList<>();
        for(int i=0; i<10; i++) {
            Account account = new Account((long) i, "zhangsan" + i, null, null, null, null, "张三"+i);
            list.add(account);
        }
        out.write(mapper.writeValueAsString(list));
    }

    /**
     * 参数映射
     * @param request
     * @param clazz
     * @return
     * @throws Exception
     */
    private Account wrapParam(HttpServletRequest request, Class<? extends Account> clazz) throws Exception{
        String requestMethod = request.getMethod();
        ObjectMapper mapper = new ObjectMapper();
        Account account = null;
        if("POST".equals(requestMethod.toUpperCase())) {
            InputStream is = request.getInputStream();
            account = mapper.readValue(is, clazz);
        } else if("GET".equals(requestMethod.toUpperCase())) {
            Map<String, String[]> parameterMap = request.getParameterMap();
            account = clazz.newInstance();
            // 参数封装
            for (String key : parameterMap.keySet()) {
                Field field = account.getClass().getDeclaredField(key);
                if(field != null) {
                    field.setAccessible(true);
                    field.set(account, parameterMap.get(key)[0]);
                }
            }
        }
        return account;
    }
}
